﻿var app = angular.module('myAPP');
app.controller('LoginController', function ($scope, $location, $http, $modal, $cookieStore) {

    $scope.init = function () {
        //    console.log($scope.original);
        $scope.submitted = false;
        //$scope.username = 'test';
        var chkRememberMe = $cookieStore.get('RememberMe');
        if (chkRememberMe == 'A') {
            var varUsername = $cookieStore.get('Username');
            var varPassword = $cookieStore.get('Password');
            $scope.username = varUsername;
            $scope.password = varPassword;
            checkCookieData(varUsername, varPassword);
            $scope.chkRememberMe = true;
        }
        else {
            $scope.chkRememberMe = false;
            checkCookieData('', '');
        }

        $scope.$parent.doselProfile();
    };



    $scope.doLogin = function (valid) {
        if (valid) {
            var varUsername = $scope.username;
            var varPassword = $scope.password;
            //console.log(varUserName);
            //console.log(varPassword);
            $http({
                url: "../../PaydSw.asmx/CheckUserLogin",
                dataType: 'json',
                method: 'POST',
                data: {
                    username: varUsername,
                    password: varPassword
                },
                headers: {
                    "Content-Type": "application/json"
                }
            }).success(function (response) {
                var data = JSON.parse(response.d);
                if (data.Result != 0) {

                    $scope.$parent.checkExistLogin();
                    $scope.$parent.doselProfile();
                    //console.log($scope.chkRememberMe);
                    if ($scope.chkRememberMe == true) {
                        $cookieStore.put("Username", varUsername);
                        $cookieStore.put("Password", varPassword);
                        $cookieStore.put("RememberMe", "A");
                    }
                    else {
                        $cookieStore.remove('Username');
                        $cookieStore.remove('Password');
                        $cookieStore.remove('RememberMe');
                    }
                    $location.path('/Profile');
                }
                else {
                    $scope.$parent.openAlert(0, "ข้อความเตือน", "รหัสผู้ใช้งานหรือรหัสผ่านไม่ถูกต้อง");
                }
            })
            .error(function (error) {
                var dataError = JSON.parse(error.responseText);
                var ErrorMessage = 'error at CheckUserLogin at LoginController ' + dataError.ExceptionType + ' ' + dataError.Message + ' ' + dataError.StackTrace;
                alert(ErrorMessage);
                $scope.$parent.doselHome();
                $location.path('/home');
            });
        }
        else {
            $scope.submitted = true;
        }
    };



    $scope.docheckRegister = function () {
        $scope.$parent.doselProfile();
        $http({
            url: "../../PaydSw.asmx/CheckUserExistLogin",
            dataType: 'json',
            method: 'POST',
            data: '',
            headers: {
                "Content-Type": "application/json"
            }
        }).success(function (response) {
            var data = JSON.parse(response.d);

            if (data.Result != 0) {
                $location.path('/Profile');
            }
            else {
                $location.path('/register');
            }
        })
        .error(function (error) {
            var dataError = JSON.parse(error.responseText);
            var ErrorMessage = 'error at CheckUserExistLogin at LoginController ' + dataError.ExceptionType + ' ' + dataError.Message + ' ' + dataError.StackTrace;
            alert(ErrorMessage);
            $scope.activeHomeIcon = true;
            $scope.activeSelHomeIcon = true;
            $location.path('/home');
        });
    };

    $scope.openForgetPassword = function () {
        var modalInstance = $modal.open({
            templateUrl: '/UI/Modal/ModalForgetPassword.aspx',
            //templateUrl: '/PaydSystem/UI/Modal/ModalForgetPassword.aspx',
            controller: 'ModalForgetPasswordController',
            size: 'lg',
            backdrop  : 'static',
            keyboard  : false
        });
        modalInstance.result.then(function (result) {
            if (result == 1) {
                $scope.$parent.openAlert(0, "ส่งอีเมล์ได้สำเร็จ", "ระบบได้ส่งอีเมล์ไปที่อีเมล์ของท่านแล้ว,โปรดเช็คที่อีเมล์");
            }
            else {
                if (result == 0) {
                    $scope.$parent.openAlert(0, 'แจ้งเตือน', 'ไม่สามารถแจ้งเปลี่ยน Password,โปรดเช็ค');
                }
                else if (result == 2) {
                    $scope.$parent.openAlert(0, 'แจ้งเตือน', 'อีเมล์นี้ไม่มีในระบบ,โปรดเช็ค');
                }
                else if (result == 3) {
                    $scope.$parent.openAlert(0, 'แจ้งเตือน', 'อีเมล์นี้รอการ activate,โปรดเช็ค');
                }
                else if (result == 4) {
                    $scope.$parent.openAlert(0, 'แจ้งเตือน', 'อีเมล์นี้ยังไม่สมัครสมาชิก,โปรดเช็ค');
                }
            }
        }, function () {

        });

    };
});